package com.taptech.doufu.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity;
import com.taptech.doufu.ui.adapter.CommonFragmentPagerAdapter;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.constant.UmengEventName;
import com.taptech.doufu.bean.DFHomeCategoryBean;
import com.taptech.doufu.ui.fragment.CategoryFragment;
import com.taptech.doufu.util.TMAnalysis;
import com.taptech.doufu.ui.view.TTHomeViewPager;
import com.taptech.doufu.ui.view.tablayout.TabLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

public class ChoiceCategoryActivity extends DiaobaoBaseActivity {

    private TabLayout tlTitle;
    private TTHomeViewPager mViewPager;
    private View laySearch;
    List<Fragment> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choice_category_layout);
        laySearch = findViewById(R.id.laySearch);
        laySearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(ChoiceCategoryActivity.this, HomeSearchActivity.class);
                startActivity(intent);
            }
        });
        tlTitle = (TabLayout) findViewById(R.id.tlTitle);
        mViewPager = (TTHomeViewPager) findViewById(R.id.mViewPager);
        mViewPager.setOffscreenPageLimit(10);
        int normalColor = getResources().getColor(R.color.text_color_2);
        int selectColor = getResources().getColor(R.color.text_color_1);
        if (WeMediaApplication.getInstance().isDayNightMode_Night) {
            normalColor = getResources().getColor(R.color.text_dark);
            selectColor = getResources().getColor(R.color.text_dark);
        }
        tlTitle.setTabTextColors(normalColor,selectColor);
        tlTitle.setSelectedTabIndicatorColor(selectColor);
        tlTitle.setTabMode(TabLayout.MODE_SCROLLABLE);
        String data = getIntent().getStringExtra(Constant.DATA);
        List<DFHomeCategoryBean.Item> items = JSON.parseArray(data, DFHomeCategoryBean.Item.class);
        if (items != null && items.size() > 0) {
            String[] titles = new String[items.size()];
            for (int i = 0; i < items.size(); i++) {
                DFHomeCategoryBean.Item item = items.get(i);
                if (item == null) {
                    continue;
                }
                titles[i] = item.getTitle();
                boolean loadDataNow;
                String sexType;
                if (i == 0) {
                    loadDataNow = true;
                    sexType = "1";
                }else{
                    loadDataNow = false;
                    sexType = "3";
                }

                list.add(CategoryFragment.Companion.newInstance(item.getUrl(),sexType,loadDataNow));
            }

            CommonFragmentPagerAdapter adapter = new CommonFragmentPagerAdapter(getSupportFragmentManager(), list, titles);
            mViewPager.setAdapter(adapter);
            tlTitle.setupWithViewPager(mViewPager);
            mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    HashMap<String, String> map = new HashMap<>();
                    map.put("position", String.valueOf(position));
                    TMAnalysis.event(ChoiceCategoryActivity.this, UmengEventName.JINGXUAN_CATEGORY_TAB,map);
                    Fragment fragment = list.get(position);
                    if (fragment instanceof CategoryFragment) {
                        ((CategoryFragment) fragment).initData();
                    }
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
        }
    }
}
